<?php

class FP_Application_Subsystem_Cache implements FP_Application_Subsystem_Interface {

    public static $enabled = false;
    public static $manager = null;

    protected $_config = null;

    public function init($pConfig=null){

	$this->_config = $pConfig;

	self::$enabled = $this->_config->enabled;

	if (self::$enabled){

	    $manager = new Zend_Cache_Manager();

	    $cache = array(
		'frontend' => array(
		    'name' => 'Core',
		    'options' => array(
			'lifetime' =>  $this->_config->lifetime,
			'automatic_serialization' => true
		    )
		),
		'backend' => array(
		    'name' => 'File',
		    'options' => array(
			'cache_dir' => FP_Application::get('cachedir')
		    )
		)
	    );

	    $manager->setCacheTemplate('core', $cache);

	    self::$manager = $manager;

	}
    }

}

function get_cache_interface($pName){
    if (FP_Application_Subsystem_Cache::$enabled){
	$manager = Zend_Registry::get('cache');

	return $manager->getCache($pName);
    } else
	return null;
}

function get_cache($pKey, $pName='core'){
    if (FP_Application_Subsystem_Cache::$enabled){
	$cache = get_cache_interface($pName);

	if ($cache){

	    $val = $cache->load(md5($pKey));
	    return $val;

	}
    }

    return null;
}

function set_cache($pVal, $pKey, $pName='core'){
    if (FP_Application_Subsystem_Cache::$enabled){
	$cache = get_cache_interface($pName);

	if ($cache){

	    $cache->save($pVal, md5($pKey));

	}
    }
}
?>